﻿namespace Ultima.Xna.Data
{
    public class StaticTileList
    {
        private static StaticTile[] _emptyTiles = new StaticTile[0];

        private int _count;
        private StaticTile[] _staticTiles;

        public StaticTileList()
        {
            _count = 0;
            _staticTiles = new StaticTile[8];
        }

        public int Count
        {
            get { return _count; }
        }

        public void AddRange(StaticTile[] tiles)
        {
            if ((_count + tiles.Length) > _staticTiles.Length)
            {
                StaticTile[] old = _staticTiles;

                _staticTiles = new StaticTile[(_count + tiles.Length) * 2];

                for (int i = 0; i < old.Length; ++i)
                {
                    _staticTiles[i] = old[i];
                }
            }

            for (int i = 0; i < tiles.Length; ++i)
            {
                _staticTiles[_count++] = tiles[i];
            }
        }

        public void Add(short id, sbyte z)
        {
            if ((_count + 1) > _staticTiles.Length)
            {
                StaticTile[] old = _staticTiles;

                _staticTiles = new StaticTile[old.Length * 2];

                for (int i = 0; i < old.Length; ++i)
                {
                    _staticTiles[i] = old[i];
                }
            }

            _staticTiles[_count].ID = id;
            _staticTiles[_count].Z = z;

            _count++;
        }

        public StaticTile[] ToArray()
        {
            if (_count == 0)
            {
                return _emptyTiles;
            }

            StaticTile[] staticTiles = new StaticTile[_count];

            for (int i = 0; i < _count; ++i)
            {
                staticTiles[i] = _staticTiles[i];
            }

            _count = 0;

            return staticTiles;
        }
    }
}